package com.example.xiaoyuan.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xiaoyuan.mapper.AdminMapper;
import com.example.xiaoyuan.pojo.Admin;
import com.example.xiaoyuan.pojo.LoginForm;
import com.example.xiaoyuan.service.AdminService;
import com.example.xiaoyuan.utils.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("adminServiceImpl")
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Override
    public Admin login(LoginForm loginForm) {

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", loginForm.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));

        Admin admin = baseMapper.selectOne(queryWrapper);

        return admin;
    }

    @Override
    public Admin getAdminById(Long userId) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);

        Admin admin = baseMapper.selectOne(queryWrapper);

        return admin;
    }

    @Override
    public IPage<Admin> getAdminsByOpr(Page<Admin> adminPage, String adminName) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(adminName !=null, Admin::getName, adminName);

        queryWrapper.orderByAsc(Admin::getId);

        Page<Admin> page = baseMapper.selectPage(adminPage, queryWrapper);
        return page;
    }
}
